package com.doge.service.admin.brand;

import com.alibaba.druid.util.StringUtils;
import com.doge.bean.bo.brand.BrandBo;
import com.doge.bean.bo.brand.BrandCreateBo;
import com.doge.bean.pojo.brand.Brand;
import com.doge.bean.pojo.brand.BrandExample;
import com.doge.bean.vo.brand.BrandResultVo;
import com.doge.exception.Param402Exception;
import com.doge.mapper.BrandMapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
import java.util.Random;

/**
 * @program: doge
 * @description: 用于制造商相关的服务
 * @author: Mr.Jiang
 * @create: 2021-11-30 10:31
 **/
@Service
public class BrandServiceImpl implements BrandService{
    @Autowired
    BrandMapper brandMapper;


    @Override
    public BrandResultVo list(BrandBo brandBo, Integer id, String name) {
        PageHelper.startPage(brandBo.getPage(), brandBo.getLimit());
        BrandExample brandExample = new BrandExample();
        //默认给的值，如果没穿参数就是这个
        if(id!=-1){
            brandExample.createCriteria().andIdEqualTo(id);
        }
        if(!StringUtils.isEmpty(name)){
            brandExample.createCriteria().andNameLike("%"+name+"%");
        }
        brandExample.createCriteria().andDeletedEqualTo(false);
        brandExample.setOrderByClause(brandBo.getSort()+" "+brandBo.getOrder());
        List<Brand> brands = brandMapper.selectByExample(brandExample);
        PageInfo info = new PageInfo(brands);
        int pages = info.getPages();
        long total = info.getTotal();
        BrandResultVo brandResultVo = new BrandResultVo();
        brandResultVo.setTotal((int) total);
        brandResultVo.setPages(pages);
        brandResultVo.setLimit(brandBo.getLimit());
        brandResultVo.setPage(brandBo.getPage());
        brandResultVo.setList(brands);
        return brandResultVo;
    }

    @Override

    public Brand create(BrandCreateBo brandCreateBo) throws Exception {
        //判断价格数量

        Brand brand = new Brand();
        brand.setName(brandCreateBo.getName());
        brand.setPicUrl(brandCreateBo.getPicUrl());
        brand.setFloorPrice(BigDecimal.valueOf(brandCreateBo.getFloorPrice()));
        brand.setAddTime(new Date());
        brand.setUpdateTime(new Date());
        brand.setDesc(brandCreateBo.getDesc());
        brand.setDeleted(false);
        Random r = new Random();
        int i = r.nextInt(100);
        brand.setSortOrder((byte) i);
        brandMapper.insertBrandGetId(brand);
        return brand;
    }

    @Override
    public Brand update(Brand brand) throws Param402Exception {
        brandMapper.updateByPrimaryKey(brand);
        Brand brandReturn = brandMapper.selectByPrimaryKey(brand.getId());
        brandReturn.setSortOrder(null);
        brandReturn.setDeleted(null);
        return brandReturn;
    }

    @Override
    public void delete(Integer id) {
        Integer i=brandMapper.delete(id,true);
    }
}
